package com.qiongyou.activity;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import com.qiongyou.adapter.GridAdapter;
import com.qiongyou.utils.HttpUtils;
import com.qiongyou.utils.UrlUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CommunityFragment extends Fragment implements AdapterView.OnItemClickListener {
    private List<Map<String,String>> totalList=null;
    private GridAdapter adapter=null;
    private GridView gridView1;//历史浏览
    private GridView gridView2;











    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_community,container,false);

        initView(view);
        loadData();
        return view;

    }
    public void initView(View view){

        gridView1= ((GridView) view.findViewById(R.id.gridView1));
        gridView2= ((GridView) view.findViewById(R.id.gridView2));
        totalList=new ArrayList<Map<String,String>>();
        adapter=new GridAdapter(totalList,getActivity());
        //设置适配器
        gridView1.setAdapter(adapter);
        gridView2.setAdapter(adapter);

        //设置监听
        gridView1.setOnItemClickListener(this);
        gridView2.setOnItemClickListener(this);


    }
    //下载数据
    public void loadData(){
        try {
            String s = HttpUtils.httpGet(UrlUtils.COMMUNITY);
            List<Map<String, String>> jsonString = getJsonString(s);
            if (jsonString!=null) {
                totalList.addAll(jsonString);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //json解析
    public List<Map<String,String>> getJsonString(String s){
            List<Map<String,String>> list=new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(s);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String ,String> map=new HashMap<>();
                JSONObject data=jsonArray.getJSONObject(i);
                map.put("title",data.getString("name"));
                JSONArray group=data.getJSONArray("group");
                for (int j = 0; j <group.length(); j++) {
                    JSONObject groups=group.getJSONObject(j);
                    map.put("name",groups.getString("name"));
                    map.put("photo",groups.getString("photo"));
                    map.put("total_threads",groups.getString("total_threads"));
                }
                    list.add(map);
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(getActivity(),"成功",Toast.LENGTH_SHORT).show();
    }
}
